1680E - Moving Chips - CodeForces Solution


bitmasks dp greedy *2000

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
#define mp make_pair
#define fi first
#define se second
#define rep(i,a,b) for(int (i)=(a);(i)<(b); ++i)
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

//Replace I and O with the input and output type
typedef vector<string> I;
typedef int O;

O smart(I &input) {
    int l = 0, r = input[0].size()-1;
    while(input[0][l] != '*' and input[1][l] != '*')
        ++l;
    while(input[0][r] != '*' and input[1][r] != '*')
        --r;

    vector<string> fixed(2);
    fixed[0] = input[0].substr(l, r-l+1);
    fixed[1] = input[1].substr(l, r-l+1);

    vector<vi> dp(2, vi(fixed[0].size()));
    dp[0][0] = fixed[1][0] == '*';
    dp[1][0] = fixed[0][0] == '*';
    for(int i=1;i<fixed[0].size();++i) {
        dp[0][i] = min(2+dp[1][i-1], 1+dp[0][i-1]+(fixed[1][i] == '*'));
        dp[1][i] = min(2+dp[0][i-1], 1+dp[1][i-1]+(fixed[0][i] == '*'));
    }
    return min(dp[0].back(), dp[1].back());
}


O dumb(I &input) {
}

//Read input from the keyboard
I get() {
    int n;
    cin>>n;
    vector<string> grid(2);
    cin >> grid[0] >> grid[1];
    return grid;
}

//Generate random test cases
I gen() {
;
}

//Print input to the screen
void print(I &input) {
    cout << "Input\n-------\n";
    cout << input[0].size() << endl;
    cout << input[0] << endl;
    cout << input[1] << endl;
}

//Print output to the screen
void print(O &output) {
    cout << output << endl;
}

int main() {
    cin.tie(0) -> sync_with_stdio(0);
    int t = 1;
    cin>>t;
    while(t--) {
        I input = get();//replace with gen()
        O s = smart(input);//O d = dumb(forDumb);

        print(s);


    }
}


Comments

Submit
0 Comments
More Questions

1217. Minimum Cost to Move Chips to The Same Position
347. Top K Frequent Elements
1503. Last Moment Before All Ants Fall Out of a Plank
430. Flatten a Multilevel Doubly Linked List
1290. Convert Binary Number in a Linked List to Integer
1525. Number of Good Ways to Split a String
72. Edit Distance
563. Binary Tree Tilt
1306. Jump Game III
236. Lowest Common Ancestor of a Binary Tree
790. Domino and Tromino Tiling
878. Nth Magical Number
2099. Find Subsequence of Length K With the Largest Sum
1608A - Find Array
416. Partition Equal Subset Sum
1446. Consecutive Characters
1618A - Polycarp and Sums of Subsequences
1618B - Missing Bigram
938. Range Sum of BST
147. Insertion Sort List
310. Minimum Height Trees
2110. Number of Smooth Descent Periods of a Stock
2109. Adding Spaces to a String
2108. Find First Palindromic String in the Array
394. Decode String
902. Numbers At Most N Given Digit Set
221. Maximal Square
1200. Minimum Absolute Difference
1619B - Squares and Cubes
1619A - Square String